package edu.uic.kott.activities;

import edu.uic.keyoutcomestrackertool.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class StudentProfileEdit extends Activity {

	EditText name;
	EditText studentID;
	EditText schoolName;
	EditText studentEmail;
	EditText major;

	public static final String StudentProfile = "studentProfile";
	public static final String FirstName = "firstName";
	public static final String LastName = "lastName";
	public static final String StudentID = "studentID";
	public static final String SchoolName = "schoolName";
	public static final String StudentEmail = "studentEmail";
	public static final String Major = "major";

	static SharedPreferences studentProfile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.student_profile_edit);

		name = (EditText) findViewById(R.id.profile_edit_name);		
		studentID = (EditText) findViewById(R.id.profile_edit_studentid);
		schoolName = (EditText) findViewById(R.id.profile_edit_schoolname);
		studentEmail = (EditText) findViewById(R.id.profile_edit_studentemail);
		major = (EditText) findViewById(R.id.profile_edit_major);

		studentProfile = getSharedPreferences(StudentProfile, Context.MODE_PRIVATE);

		if (studentProfile.contains(FirstName)) {
			name.setText(studentProfile.getString(FirstName, ""));
		}

		if (studentProfile.contains(StudentID)) {
			studentID.setText(studentProfile.getString(StudentID, ""));
		}

		if (studentProfile.contains(SchoolName)) {
			schoolName.setText(studentProfile.getString(SchoolName, ""));
		}

		if (studentProfile.contains(StudentEmail)) {
			studentEmail.setText(studentProfile.getString(StudentEmail, ""));
		}

		if (studentProfile.contains(Major)) {
			major.setText(studentProfile.getString(Major, ""));
		}

	}

	public void profileSave() {
		String fn = name.getText().toString();
		String id = studentID.getText().toString();
		String sn = schoolName.getText().toString();
		String se = studentEmail.getText().toString();
		String m = major.getText().toString();

		Editor editor = studentProfile.edit();
		editor.putString(FirstName, fn);
		editor.putString(StudentID, id);
		editor.putString(SchoolName, sn);
		editor.putString(StudentEmail, se);
		editor.putString(Major, m);

		editor.commit();	
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_save_profile, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		profileSave();//not sure
		startActivity(new Intent(StudentProfileEdit.this, StudentProfileView.class));
		return super.onOptionsItemSelected(item);
	}
}
